﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using personalplaner.business.extensions;
using System.Windows.Media;

namespace personalplaner.business.dienstplan.plancollection
{
	public class OverviewRow : ListBase<PlanItem>, IDienstPlanRow
	{
		#region IDienstPlanRow Members

		public void Initialize(DateTime beginDate, IList<PlanItem> items = null)
		{
			int cnt = DateTime.DaysInMonth(beginDate.Year, beginDate.Month);
			for (int i = 0; i < cnt; i++)
				Add(new PlanItem
				{
					Datum = new DateTime(beginDate.Year, beginDate.Month, i + 1),
					Color = RowColor,
					AbteilungId = this.AbteilungId
				});



			if (items == null)
				return;

			//foreach (var itm in items)
			//{
			while (items.Count > 0)
			{
				var itm = items[0];
				foreach (var planitem in this)
				{
					if (itm.Datum == planitem.Datum)
					{
						if (itm.Color == null || itm.Color == Colors.Transparent)
							itm.Color = planitem.Color;

						itm.AbteilungId = AbteilungId;

						Replace(planitem, itm);
						items.Remove(itm);
						break;
					}
				}
			}
		}

		int _abteilungid;
		public int AbteilungId
		{
			get
			{
				return _abteilungid;
			}
			set
			{
				_abteilungid = value;
				RaisePropertyChanged("AbteilungId");
			}
		}

		string _abteilung;
		public string Abteilung
		{
			get
			{
				return _abteilung;
			}
			set
			{
				_abteilung = value;
				RaisePropertyChanged("Abteilung");
			}
		}

		int _planSchemaId;
		public int PlanSchemaId
		{
			get
			{
				return _planSchemaId;
			}
			set
			{
				_planSchemaId = value;
				RaisePropertyChanged("PlanSchemaId");
			}
		}

		public int? Pensum
		{
			get
			{
				return null;
			}
			set
			{
			}
		}

		public double? Vortrag
		{
			get
			{
				return null;
			}
			set
			{
			}
		}

		double? _soll;
		public double? Soll
		{
			get { return _soll; }
			set
			{
				_soll = value;
				RaisePropertyChanged("Soll");
				RaisePropertyChanged("Differenz");
			}
		}

		double? _haben;
		public double? Haben
		{
			get { return _haben; }
			set
			{
				_haben = value;
				RaisePropertyChanged("Haben");
				RaisePropertyChanged("Differenz");
			}
		}

		double? _anpassung;
		public double? Anpassung
		{
			get { return _anpassung; }
			set
			{
				_anpassung = value;
				RaisePropertyChanged("Anpassung");
				RaisePropertyChanged("Differenz");
			}
		}

		public double? Differenz
		{
			get
			{
				if (/*Vortrag != null && */Haben != null && Soll != null && Anpassung != null)
					return (/*(double)Vortrag + */(((double)Haben + Ferien) - (double)Soll) + (double)Anpassung).Round();

				return 0.0;
			}
		}

		public double Saldo
		{
			get
			{
                if (/*Vortrag != null && */Haben != null && Soll != null && Anpassung != null)
                {
                    //return (((double)Haben + Ferien) - (double)Soll + (double)Anpassung).Round();
                    return ((double)Haben + Ferien).Round();
                }
				return 0.0;
			}
		}

		double _ferien;
		public double Ferien
		{
			get { return _ferien; }
			set
			{
				_ferien = value;
				RaisePropertyChanged("Ferien");
			}
		}

		Color _color;
		public Color RowColor
		{
			get
			{
				return _color;
			}
			set
			{
				_color = value;
				RaisePropertyChanged("RowColor");
			}
		}

		Color _headercolor;
		public Color HeaderColor
		{
			get
			{
				return _headercolor;
			}
			set
			{
				_headercolor = value;
				RaisePropertyChanged("HeaderColor");
			}
		}

		#endregion
	}
}
